package net.stogi.unicore.beyond.ui.server.common.action.handler;

import java.util.List;

import net.stogi.common.api.exception.BusinessException;
import net.stogi.common.gwt.dispatch.server.core.ActionHandler;
import net.stogi.common.gwt.dispatch.shared.exception.ActionException;
import net.stogi.unicore.beyond.api.model.TaskDefinition;
import net.stogi.unicore.beyond.api.service.TaskService;
import net.stogi.unicore.beyond.ui.shared.common.action.model.LoadTaskDefinitionsAction;
import net.stogi.unicore.beyond.ui.shared.common.action.model.LoadTaskDefinitionsResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("UnicoreBeyondUi.Dispatch.LoadTaskDefinitionsActionHandler")
public class LoadTaskDefinitionsActionHandler implements ActionHandler<LoadTaskDefinitionsAction, LoadTaskDefinitionsResult> {

	@Autowired
	private TaskService taskService;
	
	@Override
	public LoadTaskDefinitionsResult execute(LoadTaskDefinitionsAction action) throws ActionException {
		try {
			List<TaskDefinition> taskDefinitions = taskService.listTaskDefinitions();
			return new LoadTaskDefinitionsResult(taskDefinitions);
		}
		catch (BusinessException e) {
			throw new ActionException(e);
		}
	}

	@Override
	public Class<LoadTaskDefinitionsAction> getActionType() {
		return LoadTaskDefinitionsAction.class;
	}

	@Override
	public void rollback(LoadTaskDefinitionsAction action, LoadTaskDefinitionsResult result) throws ActionException {}
}
